<include file='Index/header' />
<div class="row">
	<br>
	<div class="col-sm-12" >
    	<button type="button" data-toggle="modal" class="btn btn-primary" onclick="editNew()">添加新商品模板</button>
    	<br><br>
  </div>

	<div class="form-group">
		<div class="col-sm-2"></div>
		<div class="col-sm-1" style="padding-top:5px;">
			<label for="key" class="control-label">关键字：</label>
		</div>
		<div class="col-sm-4">
			<input id="key" name="key" class="form-control"></input>
		</div>
	</div>
	
	<div class="col-sm-12">
		<br>
		<div class="col-sm-2">
			<ul id="product_class_option_tree" class="ztree"></ul>
		</div>
		<div class="col-sm-10">
			<table id="product-template-table" class="table table-hover table-bordered table-striped table-striped">
				<thead>
					<tr>
						<td>大类</td>
						<td>中类</td>
						<td>子类</td>
						<td>关键字</td>
						<td>编号</td>
						<td>商品名称</td>
						<td>条形码货号</td>
						<td>价格</td>
						<td>上架</td>
						<td>排序</td>
						<td>操作</td>
					</tr>
				</thead>
				<tbody>

				</tbody>
			</table>
		</div>
	</div>
	<br>
</div>

<include file='Index/footer' />
<include file='Product/product-template-detail' />
<script>
$(document).ready(function() {
	loadProductClassTree('product_class_option_tree', true, true);
	loadTable();		
	loadProductClassTree('product_class_tree', false, false);
});
var table;

function loadTable(){
	table = $('#product-template-table').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"sPaginationType" : "input",
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          }, 
		"ajax":'{:U("Admin/Product/getProductTemplate/")}',
		"columns": [
					{ "data":'class1'},
					{ "data":'class2'},
					{ "data":'class3'},
					{ "data":'key'},
	        { "data": 'id' },
	        { "data": 'name' },
	        { "data": 'barcode' },
	        { "data": 'kj_price' },
	        { "data": 'status' },
	        { "data": 'index' },
	        { "data": 'btn' }
    ],
    "columnDefs": [
	    {
	    	"targets": [0, 1, 2, 3],
	      "visible":false
	    },
	    {
	    	"targets": 4,
	    	"render": function (data, type, row, table) {
	    		return table.row + 1;
	      }
	    },
	    {
	    	"targets": -3,
	    	"render": function (data, type, row, table) {
	    		return data == '0'? "是":"否";
	      }
	    },
	    {
	    	"targets": -1,
	    	"data": null,
	    	"render": function (data, type, row, table) {
	    		var btn = '下架';
	    		var status = '1';
	    		if(row.status != '0'){
						btn = '上架';
						status = '0';
	    		}
	        return '<button class="btn btn-info" onclick="edit(\''+row.product_template_id+'\')">编辑</button> <button class="btn btn-warning" onclick="modStatus(\''+row.product_template_id+'\',\''+status+'\')">'+btn+'</button>';
	      },
	    }
    ]
	});
	
	$("#key").keyup(function(){
		table.column(3).search(this.value).draw();
  });
}
function editNew(){
	$('#savePriceBtn').attr('style', 'display:none');
	$('#product-template-detail').modal({
		backdrop : "static"
	});
}
function edit(product_template_id){
	//编辑按钮点击，弹出编辑框
	$.ajax({
			url: '{:U("Admin/Product/getProductTemplate/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"product_template_id":product_template_id
			}
		})
		.success(function(data){
			var a_repository_id = [], b_repository_id = [];
			for (var i = 0; i < data.product.length; i++) {
				a_repository_id.push(data.product[i].a_repository_id);
				b_repository_id.push(data.product[i].b_repository_id);
			};
			$('#a_repository_id').multiselect('select', a_repository_id);
			$('#b_repository_id').multiselect('select', b_repository_id);

			var treeObj = $.fn.zTree.getZTreeObj("product_class_tree");
			//var parentNode = treeObj.getNodeByParam("id", data.class1+'-'+data.class2);
			var node = treeObj.getNodeByParam("product_class_id", data.class3);
			treeObj.selectNode(node);

			imgurl.push(data.imgurl1);
			imgurl.push(data.imgurl2);
			imgurl.push(data.imgurl3);
			smimgurl = data.smimgurl;

			$('#img').html('<img width="150px" src="'+data.imgurl1+'" />&nbsp;<img width="150px" src="'+data.imgurl2+'" />&nbsp;<img width="150px" src="'+data.imgurl3+'" />');
			$('#smimg').html('<img width="150px" src="'+data.smimgurl+'" />');

			$("input[name='product_template_id']").attr('value', data.product_template_id);
			$("input[name='name']").attr('value', data.name);
			$("input[name='subname']").attr('value', data.subname);
			$("input[name='barcode']").attr('value', data.barcode);
			$("input[name='key']").attr('value', data.key);
			$("input[name='index']").attr('value', data.index);
			$('#tag').attr('checked', 'true');
			$('#sendtype'+data.sendtype).attr('checked', 'true');
			$("input[name='jh_price']").attr('value', data.jh_price);
			$("input[name='sc_price']").attr('value', data.sc_price);
			$("input[name='kj_price']").attr('value', data.kj_price);
			$("input[name='default_amount']").attr('value', data.default_amount);
			$("input[name='warning_amount']").attr('value', data.warning_amount);
			$("input[name='origin_amount']").attr('value', data.origin_amount);
			$("input[name='scale']").attr('value', data.scale);
			$("input[name='origin_place']").attr('value', data.origin_place);
			editor.setData(data.product_detail);
			$('#savePriceBtn').attr('style', '');

			$('#product-template-detail').modal('show');
	});
}

function modStatus(product_template_id, status){
	if(!confirm("是否确认修改该模板下所有商品的状态？")){
		return;
	}
	$.ajax({
			url: '{:U("Admin/Product/updateProductStatus/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"product_template_id":product_template_id,
				"status":status
			}
		})
		.success(function(data){
			location.reload();
	});
}

function searchClass1(product_class_id){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(2).search('').draw();
  table.column(0).search(product_class_id).draw();
}
function searchClass2(product_class_id){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(2).search('').draw();
  table.column(1).search(product_class_id).draw();
}
function searchClass3(product_class_id){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(2).search('').draw();
  table.column(2).search(product_class_id).draw();
}
</script>
